package com.example.zqq.mysnake;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.zqq.mysnake.view.SnakeView;

public class MainActivity extends AppCompatActivity {

    SnakeView snakeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        snakeView = new SnakeView(this);

        setContentView(snakeView);

        //学习Gradle
        String apiUrl = BuildConfig.API_URL;
        boolean logHttpCalls = BuildConfig.LOG_HTTP_CALLS;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private long currentTime = 0;
    private static final long BACK_INTERVAL = 2000;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - currentTime > BACK_INTERVAL) {
            currentTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
//            try {
//                this.clearWallpaper();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }

            SnakeView snakeView = new SnakeView(this);
            setContentView(snakeView);
        } else {
            finish();
        }
    }
}
